﻿//-----------------------------------------------------------------------
// <copyright file="DiagnosticsServiceHost.cs" company="CodePlex">
//     Copyright (c) Doug Holland, CodePlex. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace CodePlex.Diagnostics
{
    using System;
    using System.ServiceModel;
    
    using CodePlex.Diagnostics.Services;

    using Microsoft.Data.Schema.UnitTesting;

    using Microsoft.VisualStudio.TestTools.UnitTesting;

    /// <summary>
    /// Provides a service host for the <see cref="T:CodePlex.Diagnostics.Services.DiagnosticsService"/> for use within this unit test class.
    /// </summary>
    /// <remarks>
    /// Within the Visual Studio unit testing framework only one method can be declared with the <see cref="T:AssemblyInitializeAttribute"/> inside an assembly, as such
    /// the database initialization code also resides within this class.
    /// </remarks>
    [TestClass]
    public static class DiagnosticsServiceHost
    {
        #region DiagnosticsServiceHost Private Fields

        /// <summary>
        /// Singleton instance of the service host for the <see cref="T:CodePlex.Diagnostics.Services.DiagnosticsService"/> class.
        /// </summary>
        private static ServiceHost<DiagnosticsService> instance;

        #endregion

        #region private static ServiceHost<DiagnosticsService> SingletonInstance

        /// <summary>
        /// Gets the unique singleton instance of the service host for the <see cref="T:CodePlex.Diagnostics.Services.DiagnosticsService"/> class.
        /// </summary>
        /// <value>The unique singleton instance of the service host for the <see cref="T:CodePlex.Diagnostics.Services.DiagnosticsService"/> class.</value>
        private static ServiceHost<DiagnosticsService> SingletonInstance
        {
            get
            {
                if (instance == null)
                {
                    instance = new ServiceHost<DiagnosticsService>();
                }

                return instance;
            }
        }

        #endregion

        #region public static void StartService(TestContext context)

        /// <summary>
        /// Creates an instance of the <see cref="T:CodePlex.Diagnostics.Services.ServiceHost"/> and opens the communication channel.
        /// </summary>
        /// <param name="context">Used to store information that is provided to unit tests.</param>
        [AssemblyInitialize]
        public static void StartService(TestContext context)
        {
            // see the remarks comments within the class level Xml documentation above.
            DatabaseTestClass.TestService.DeployDatabaseProject();
            DatabaseTestClass.TestService.GenerateData();

            if (SingletonInstance.State != CommunicationState.Opened)
            {
                SingletonInstance.Open();
            }
        }

        #endregion

        #region public static void StopService()

        /// <summary>
        /// Closes the communication channel if the <see cref="T:System.ServiceModel.CommunicationState"/> is not already closed.
        /// </summary>
        [AssemblyCleanup]
        public static void StopService()
        {
            if (SingletonInstance.State != CommunicationState.Closed)
            {
                SingletonInstance.Close();
            }
        }

        #endregion
    }
}
